package com.zhentao.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhentao.common.Result;
import com.zhentao.dto.UsedCarEvaluationRequest;
import com.zhentao.dto.UsedCarEvaluationResponse;
import com.zhentao.pojo.UsedCarEvaluation;
import com.zhentao.service.UsedCarEvaluationService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 二手车评估控制器
 */
@Slf4j
@RestController
@RequestMapping("/api/used-car")
@CrossOrigin
public class UsedCarEvaluationController {
    
    @Autowired
    private UsedCarEvaluationService evaluationService;
    
    /**
     * 创建二手车评估
     */
    @PostMapping("/evaluate")
    public Result<UsedCarEvaluationResponse> createEvaluation(@RequestBody UsedCarEvaluationRequest request) {
        try {
            // 参数验证
            if (request.getBrand() == null || request.getModel() == null || 
                request.getYear() == null || request.getMileage() == null) {
                return Result.badRequest("请填写完整的车辆信息");
            }
            
            UsedCarEvaluationResponse response = evaluationService.createEvaluation(request);
            return Result.success("评估完成", response);
            
        } catch (Exception e) {
            log.error("创建评估失败: {}", e.getMessage(), e);
            return Result.error("评估失败: " + e.getMessage());
        }
    }
    
    /**
     * 快速评估（简化版）
     */
    @PostMapping("/quick-evaluate")
    public Result<UsedCarEvaluationResponse> quickEvaluation(
            @RequestParam String brand,
            @RequestParam String model,
            @RequestParam Integer year,
            @RequestParam Double mileage) {
        try {
            UsedCarEvaluationResponse response = evaluationService.quickEvaluation(brand, model, year, mileage);
            return Result.success("快速评估完成", response);
            
        } catch (Exception e) {
            log.error("快速评估失败: {}", e.getMessage(), e);
            return Result.error("快速评估失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取评估详情
     */
    @GetMapping("/detail/{id}")
    public Result<UsedCarEvaluationResponse> getEvaluationDetail(@PathVariable Long id) {
        try {
            UsedCarEvaluationResponse response = evaluationService.getEvaluationDetail(id);
            return Result.success("获取成功", response);
            
        } catch (Exception e) {
            log.error("获取评估详情失败: {}", e.getMessage(), e);
            return Result.error("获取评估详情失败: " + e.getMessage());
        }
    }
    
    /**
     * 分页查询评估记录
     */
    @GetMapping("/page")
    public Result<IPage<UsedCarEvaluation>> getEvaluationPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(required = false) Long userId) {
        try {
            IPage<UsedCarEvaluation> page = evaluationService.getEvaluationPage(pageNum, pageSize, userId);
            return Result.success("查询成功", page);
            
        } catch (Exception e) {
            log.error("分页查询评估记录失败: {}", e.getMessage(), e);
            return Result.error("查询失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取用户评估历史
     */
    @GetMapping("/history/{userId}")
    public Result<List<UsedCarEvaluation>> getUserEvaluationHistory(@PathVariable Long userId) {
        try {
            List<UsedCarEvaluation> history = evaluationService.getUserEvaluationHistory(userId);
            return Result.success("获取历史记录成功", history);
            
        } catch (Exception e) {
            log.error("获取用户评估历史失败: {}", e.getMessage(), e);
            return Result.error("获取历史记录失败: " + e.getMessage());
        }
    }
    
    /**
     * 删除评估记录
     */
    @DeleteMapping("/{id}")
    public Result<String> deleteEvaluation(@PathVariable Long id) {
        try {
            boolean success = evaluationService.deleteEvaluation(id);
            if (success) {
                return Result.success("删除成功");
            } else {
                return Result.error("删除失败");
            }
            
        } catch (Exception e) {
            log.error("删除评估记录失败: {}", e.getMessage(), e);
            return Result.error("删除失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取热门品牌
     */
    @GetMapping("/brands/popular")
    public Result<List<String>> getPopularBrands(@RequestParam(defaultValue = "10") Integer limit) {
        try {
            List<String> brands = evaluationService.getPopularBrands(limit);
            return Result.success("获取热门品牌成功", brands);
            
        } catch (Exception e) {
            log.error("获取热门品牌失败: {}", e.getMessage(), e);
            return Result.error("获取热门品牌失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取品牌下的车型
     */
    @GetMapping("/models/{brand}")
    public Result<List<String>> getModelsByBrand(@PathVariable String brand) {
        try {
            List<String> models = evaluationService.getModelsByBrand(brand);
            return Result.success("获取车型成功", models);
            
        } catch (Exception e) {
            log.error("获取品牌车型失败: {}", e.getMessage(), e);
            return Result.error("获取车型失败: " + e.getMessage());
        }
    }
    
    /**
     * 获取评估统计信息
     */
    @GetMapping("/statistics")
    public Result<Object> getEvaluationStatistics() {
        try {
            // 这里可以添加统计逻辑，比如总评估次数、平均价值等
            return Result.success("获取统计信息成功", null);
            
        } catch (Exception e) {
            log.error("获取统计信息失败: {}", e.getMessage(), e);
            return Result.error("获取统计信息失败: " + e.getMessage());
        }
    }
}
